package com.sskj.api.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sskj.api.domain.Dict;
import com.sskj.api.domain.DictItem;
import com.sskj.api.service.IDictItemService;
import com.sskj.api.service.IDictService;
import com.sskj.api.vo.req.DictItemPageListReq;
import com.sskj.api.vo.req.DictPageListReq;
import com.sskj.common.util.ApiResponseUtil;
import com.sskj.common.vo.ApiResponse;
import com.sskj.common.vo.SimplePagination;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Tag(name = "字典模块")
@RestController
@RequestMapping("/admin/dict")
@Validated
public class DictController {

    @Autowired
    private IDictService dictService;

    @Autowired
    private IDictItemService dictItemService;

    @Operation(summary = "新增字典")
    @PostMapping("add")
    public ApiResponse<Object> add(@Valid @RequestBody Dict dict) {
        dictService.add(dict);

        return ApiResponseUtil.success();
    }

    @Operation(summary = "删除字典")
    @PostMapping("delete")
    public ApiResponse<Object> delete(@RequestParam Long id) {
        dictService.deleteById(id);

        return ApiResponseUtil.success();
    }

    @Operation(summary = "更新字典")
    @PostMapping("update")
    public ApiResponse<Object> update(@Valid @RequestBody Dict dict) {
        dictService.update(dict);

        return ApiResponseUtil.success();
    }

    @Operation(summary = "字典分页列表")
    @GetMapping("pageList")
    public ApiResponse<SimplePagination<Dict>> pageList(DictPageListReq req) {
        IPage<Dict> dictIPage = dictService.pageList(req);

        return ApiResponseUtil.success(dictIPage);
    }

    @Operation(summary = "新增字典项")
    @PostMapping("itemAdd")
    public ApiResponse<Object> itemAdd(@Valid @RequestBody DictItem dictItem) {
        dictItemService.add(dictItem);

        return ApiResponseUtil.success();
    }

    @Operation(summary = "删除字典项")
    @PostMapping("itemDelete")
    public ApiResponse<Object> itemDelete(@RequestParam Long itemId) {
        dictItemService.deleteById(itemId);

        return ApiResponseUtil.success();
    }

    @Operation(summary = "更新字典项")
    @PostMapping("itemUpdate")
    public ApiResponse<Object> itemUpdate(@Valid @RequestBody DictItem dictItem) {
        dictItemService.update(dictItem);

        return ApiResponseUtil.success();
    }

    @Operation(summary = "字典项分页列表")
    @GetMapping
    public ApiResponse<SimplePagination<DictItem>> itemPageList(DictItemPageListReq req) {
        IPage<DictItem> dictItemIPage = dictItemService.pageList(req);

        return ApiResponseUtil.success(dictItemIPage);
    }
}
